const App = getApp();
Page({
    data: {
        imgUrl:App.globalData.imgUrl,
        isLoading: true,
        reLoading: false,
        list: [],
        imgUrl:App.globalData.imgUrl
    },
    onLoad: function (options) {
        let _this = this;
        _this.getGenerateList();
    },
    toJump(e){
        // console.log(e)
        let url = e.currentTarget.dataset.url;
        wx.navigateTo({
            url: url
        });
    },
    // 获取赋能记录
    getGenerateList(page = 1) {
        let _this = this;
        App._get('Generate/lists', {page}, res => {
            _this.setData({
                list: _this.data.list.concat(res.data.list.data),
                current_page: res.data.list.current_page,
                last_page: res.data.list.last_page,
                total: res.data.list.total,
            })
        }, false, () => {
            _this.setData({
                isLoading: false,
                reLoading: false,
            })
        })
    },

    toDetail(e) {
        let _this = this;
        let key = e.currentTarget.dataset.key;
        wx.navigateTo({
            url: '/pages/generate/detail?key=' + key,
        });
    },

    // 删除记录
    delete(e) {
        let _this = this,
            item = e.currentTarget.dataset.item,
            index = e.currentTarget.dataset.index;
        wx.showModal({
            title: '友情提示',
            content: '删除后无法恢复,是否真的要删除此记录?',
            success(res) {
                if (res.confirm) {
                    _this.setData({
                        reLoading: true
                    })
                    App._get('generate/delete', {
                        id: item.generate_id
                    }, res => {
                        wx.showToast({
                            title: res.msg
                        })
                        _this.data.list.splice(index, 1);
                        _this.setData({
                            reLoading: false,
                            list: _this.data.list,
                            total: _this.data.total - 1
                        })
                    });
                }
            }
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.ajaxloading = this.selectComponent("#ajax_loading");
        if (this.data.current_page >= this.data.last_page) {
            this.ajaxloading.AjaxLoading(true);
            this.ajaxloading.AjaxNoresult(false);
            return false;
        }
        this.getGenerateList(this.data.current_page + 1);
        this.ajaxloading.AjaxLoading(false);
    },


});
